from celery import Celery
import logging
from app.config import config
from app.services.node_registry import get_node_executor

celery = Celery('workflow', broker=config['queue']['redis']['url'])
logger = logging.getLogger("workflow")
logger.setLevel(logging.INFO)

@celery.task
def execute_node(node_id: str, node_data: dict):
    logger.info(f"Executing node {node_id}: {node_data}")
    
    # 获取节点类型，默认使用现有逻辑作为 "input" 类型
    node_type = node_data.get("type", "input")
    
    try:
        executor = get_node_executor(node_type)
        result = executor(node_id, node_data)
        return result
    except ValueError as e:
        logger.error(f"Failed to execute node {node_id}: {str(e)}")
        return {"node_id": node_id, "status": "error", "message": str(e)}